﻿using StopLight.Interfaces;
using Microsoft.Practices.SLUnity;
using Stoplight.Enums;
using Stoplight.Events;
using Stoplight.Base;
using Stoplight.Interfaces;

namespace StopLight.Services 
{
    public class StoplightService : ServiceBase, IStoplightService 
    {
        private StoplightColors _currentColor = StoplightColors.Green;
        public StoplightColors CurrentColor 
        {
            get { return _currentColor; }
        }

        protected override void OnEventAggregatorSet()
        {
            EventAggregator.GetEvent<ChangeLightEvent>().Subscribe(Next);
        }

        public void Next(ChangeLightEvent e) {
            ++_currentColor;
            if (_currentColor > StoplightColors.Red) 
                _currentColor = StoplightColors.Green;

            EventAggregator.GetEvent<LightChangedEvent>().Publish(
                new LightChangedEvent() { CurrentColor = _currentColor });
 
            Logger.Log(string.Format("LIGHT CHANGED TO {0} => Published LightChangedEvent", _currentColor), Category.Debug, Priority.None);
        }
    }
}
